<?php

namespace JLPay\Services;

use JLPay\Core\OrgConfig;
use JLPay\Models\BaseData\BankListRequest;
use JLPay\Models\BaseData\BankListResponse;
use JLPay\Models\BaseData\BankBranchListRequest;
use JLPay\Models\BaseData\BankBranchListResponse;
use JLPay\Models\BaseData\MccQueryRequest;
use JLPay\Models\BaseData\MccQueryResponse;
use JLPay\Models\BaseData\RegionListRequest;
use JLPay\Models\BaseData\RegionListResponse;
use JLPay\Models\BaseData\BankCardBinMatchingRequest;
use JLPay\Models\BaseData\BankCardBinMatchingResponse;

/**
 * 公共基础数据服务
 */
class BaseDataService extends BaseService
{
    /**
     * 构造函数
     *
     * @param OrgConfig $config
     */
    public function __construct(OrgConfig $config)
    {
        parent::__construct($config);
    }

    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        $baseUrl = $this->config->getUrl();

        if ($apiPath) {
            return $baseUrl . $apiPath;
        }

        // 使用请求类中定义的API_URL常量
        if (defined($requestClass . '::API_URL')) {
            $apiUrl = constant($requestClass . '::API_URL');
            return $baseUrl . $apiUrl;
        }

        // 如果没有定义API_URL，使用默认路径
        return $baseUrl . '/open/base/data';
    }

    /**
     * 查询银行总行信息
     *
     * @param BankListRequest $request
     * @return BankListResponse
     */
    public function queryBankList(BankListRequest $request): BankListResponse
    {
        return $this->post($request, BankListResponse::class);
    }

    /**
     * 查询分支行信息
     *
     * @param BankBranchListRequest $request
     * @return BankBranchListResponse
     */
    public function queryBankBranchList(BankBranchListRequest $request): BankBranchListResponse
    {
        return $this->post($request, BankBranchListResponse::class);
    }

    /**
     * 查询MCC信息
     *
     * @param MccQueryRequest $request
     * @return MccQueryResponse
     */
    public function queryMcc(MccQueryRequest $request): MccQueryResponse
    {
        return $this->post($request, MccQueryResponse::class);
    }

    /**
     * 查询地区码信息
     *
     * @param RegionListRequest $request
     * @return RegionListResponse
     */
    public function queryRegionList(RegionListRequest $request): RegionListResponse
    {
        return $this->post($request, RegionListResponse::class);
    }

    /**
     * 查询银行卡bin信息
     *
     * @param BankCardBinMatchingRequest $request
     * @return BankCardBinMatchingResponse
     */
    public function queryBankCardBin(BankCardBinMatchingRequest $request): BankCardBinMatchingResponse
    {
        return $this->post($request, BankCardBinMatchingResponse::class);
    }
} 